package top.wilbert.collegelink.classification.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.wilbert.collegelink.classification.entity.RegionEntity;
import top.wilbert.collegelink.classification.service.RegionService;
import top.wilbert.collegelink.classification.vo.Catalog2Vo;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.R;

/**
 * @author Augus
 * @email 2531496343@qq.com
 * @date 2023-02-21 09:32:39
 */
@RestController
@RequestMapping("classification/region")
public class RegionController {
    @Autowired
    private RegionService regionService;


    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/listTree")
    public R listTree(){
        List<RegionEntity> regionEntities = regionService.listWithTree();
        return R.ok().setData(regionEntities);
    }
 

    @ResponseBody
    @GetMapping("/getCatalogJson")
    public Map<Long, List<Catalog2Vo>> getCatalogJson(){
        Map<Long, List<Catalog2Vo>> catalogJson = regionService.getCatalogJson();

        return catalogJson;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("classification:region:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = regionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{regionId}")
//    @RequiresPermissions("classification:region:info")
    public R info(@PathVariable("regionId") Long regionId){
		RegionEntity region = regionService.getById(regionId);

        return R.ok().put("region", region);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("classification:region:save")
    public R save(@RequestBody RegionEntity region){
		regionService.save(region);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("classification:region:update")
    public R update(@RequestBody RegionEntity region){
		regionService.updateById(region);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("classification:region:delete")
    public R delete(@RequestBody Long[] regionIds){
		regionService.removeByIds(Arrays.asList(regionIds));

        return R.ok();
    }

}
